package com.woniu.his.service.impl;

import com.woniu.his.dao.*;
import com.woniu.his.entity.*;
import com.woniu.his.service.OutpatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OutpatientServiceImpl implements OutpatientService {
    @Autowired
    private RegistrationInfoMapper registrationInfoMapper;

    @Autowired
    private PatientInfoMapper patientInfoMapper;

    @Autowired
    private MedicalInfoMapper medicalInfoMapper;

    @Autowired
    private HospitalizationInfoMapper hospitalizationInfoMapper;

    @Autowired
    private OperationInfoMapper operationInfoMapper;

    @Autowired
    private DrugInfoMapper drugInfoMapper;

    @Autowired
    private DrugApplyMapper drugApplyMapper;

    @Override
    public int savepatientInfo(PatientInfo patientInfo) {
        patientInfoMapper.insert(patientInfo);
        return patientInfo.getPatientId();
    }

    @Override
    public HashMap findById(int id) {
        return null;
    }

    @Override
    public List<HashMap> queryAllRegistration() {
        List<HashMap> regisList = registrationInfoMapper.queryAll();
        return regisList;
    }

    @Override
    public int updateRegStatus(String registrationId) {
        int i = registrationInfoMapper.updateRegStatus(registrationId);
        return i;
    }

    @Override
    public List<RegistrationInfo> queryRegis() {
        return registrationInfoMapper.queryRegis();
    }

    @Override
    public int saveMedicalInfo(MedicalInfo medicalInfo) {
        int insert = medicalInfoMapper.insert(medicalInfo);
        int medicalId = 0;
        if (insert==1){
            medicalId = medicalInfo.getMedicalId();
            System.out.println("获取的主键medicalId:" + medicalId);
        }
        return medicalId;
    }

    @Override
    public void updateRegisStatus(int registrationId, int i) {
        RegistrationInfo registrationInfo = new RegistrationInfo();
        registrationInfo.setRegistrationId(registrationId);
        registrationInfo.setStatus(i);
        registrationInfoMapper.updateById(registrationInfo);
    }

    @Override
    public void addHospitalizationInfo(HospitalizationInfo hpi) {
        hospitalizationInfoMapper.insert(hpi);
    }

    @Override
    public void addOperationInfo(OperationInfo operationInfo) {
        operationInfoMapper.insert(operationInfo);
    }

    @Override
    public List<Map<String, Object>> queryDrugMedical() {
//        QueryWrapper<MedicalInfo> medicalInfoQueryWrapper = new QueryWrapper<>();
//        medicalInfoQueryWrapper.eq("status", 1);
        List<Map<String, Object>> medicalInfos = medicalInfoMapper.queryDrugMedical();
        return medicalInfos;
    }

    @Override
    public List<DrugInfo> queryDrugInfo() {
        List<DrugInfo> drugInfos = drugInfoMapper.selectList(null);
        System.out.println("药品详情列表: " + drugInfos);
        return drugInfos;
    }

    @Override
    public void addDrugApply(Map drugApplyMap) {
        DrugApply drugApply = new DrugApply();
        drugApply.setMedicalId(Integer.parseInt(drugApplyMap.get("medicalId").toString()));
        drugApply.setDrugId(Integer.parseInt(drugApplyMap.get("drugId").toString()));
        drugApply.setDrugApplyCount(Integer.parseInt(drugApplyMap.get("drugApplyCount").toString()) );
        drugApply.setTotalPrice(new BigDecimal(drugApplyMap.get("totalPrice").toString()));
        drugApply.setApplyTime(new Date());

        drugApplyMapper.insert(drugApply);
    }

    @Override
    public void updateMedicalStatus(Object medicalId) {
        MedicalInfo medicalInfo = new MedicalInfo();
        medicalInfo.setMedicalId(Integer.parseInt(medicalId.toString()));
        medicalInfo.setStatus(0);
        medicalInfoMapper.updateById(medicalInfo);
    }
}
